package com.livedoc.ui.administration.components.users;

import java.util.ArrayList;
import java.util.List;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.extensions.markup.html.repeater.data.table.DefaultDataTable;
import org.apache.wicket.extensions.markup.html.repeater.data.table.IColumn;
import org.apache.wicket.extensions.markup.html.repeater.data.table.PropertyColumn;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.ResourceModel;

import com.livedoc.business.domain.entities.User;
import com.livedoc.ui.common.helpers.UsersProvider;

public class UsersInformationPanel extends Panel {

	private static final long serialVersionUID = -3541034646571557399L;

	public UsersInformationPanel(String id) {
		super(id);
	}

	@Override
	public void onInitialize() {
		super.onInitialize();

		final WebMarkupContainer usersTableContainer = new WebMarkupContainer("table-container");

		usersTableContainer.setOutputMarkupId(true);

		PropertyColumn<User, String> usernameColumn = new PropertyColumn<User, String>(new ResourceModel(
				"table.users.name"), "name");
		PropertyColumn<User, String> roleColumn = new PropertyColumn<User, String>(
				new ResourceModel("table.users.role"), "role.name");
		PropertyColumn<User, String> projectColumn = new PropertyColumn<User, String>(new ResourceModel(
				"table.users.project"), "projectsAsString");

		List<IColumn<User, String>> columns = new ArrayList<IColumn<User, String>>();
		columns.add(usernameColumn);
		columns.add(roleColumn);
		columns.add(projectColumn);

		DefaultDataTable<User, String> usersTable = new DefaultDataTable<User, String>("table-users", columns,
				new UsersProvider(), 5);
		usersTableContainer.add(usersTable);

		add(usersTableContainer);
		add(new EditUserInformationPanel("add-user-panel") {

			private static final long serialVersionUID = 1345773250199751091L;

			@Override
			protected void onSave(AjaxRequestTarget target) {
				target.add(usersTableContainer);
			}
		});

	}
}
